<template>
  <div class="chat-page">
    <!-- 聊天界面 -->
    <div class="chat-container glass-effect">
      <!-- 聊天头部 -->
      <div class="chat-header">
        <div class="ai-status">
          <span class="status-dot"></span>
          AI 助手在线
        </div>
        <div class="header-actions">
          <el-button type="text" icon="el-icon-delete" @click="clearChat">清空对话</el-button>
        </div>
      </div>

      <!-- 聊天记录区域 -->
      <div class="chat-messages" ref="messageContainer">
        <div v-if="messages.length === 0" class="welcome-message">
          <div class="ai-avatar">
            <i class="el-icon-chat-round"></i>
          </div>
          <h2>欢迎使用 AI 助手</h2>
          <p>您可以向我提问任何问题，我会尽力为您解答</p>
        </div>
        
        <div v-for="(msg, index) in messages" 
             :key="index" 
             :class="['message', msg.type]">
          <div class="message-avatar">
            <i :class="msg.type === 'user' ? 'el-icon-user' : 'el-icon-chat-round'"></i>
          </div>
          <div class="message-content">
            <div class="message-text" v-html="formatMessage(msg.content)"></div>
            <div class="message-time">{{ formatTime(msg.timestamp) }}</div>
          </div>
        </div>
      </div>

      <!-- 输入区域 -->
      <div class="chat-input">
        <el-input
          v-model="inputMessage"
          type="textarea"
          :rows="3"
          placeholder="输入您的问题..."
          @keyup.enter.native="sendMessage"
        ></el-input>
        <div class="input-actions">
          <el-button 
            type="text" 
            icon="el-icon-mic" 
            class="action-btn"
            title="语音输入">
          </el-button>
          <el-button 
            class="send-btn gradient-btn"
            :disabled="!inputMessage.trim()"
            @click="sendMessage">
            发送 <i class="el-icon-position"></i>
          </el-button>
        </div>
      </div>
    </div>

    <!-- 功能提示 -->
    <div class="features-tips">
      <div class="tip-card glass-effect" v-for="tip in tips" :key="tip.id">
        <i :class="tip.icon"></i>
        <h3>{{ tip.title }}</h3>
        <p>{{ tip.description }}</p>
      </div>
    </div>
  </div>
</template>

<script>
export default {
  name: 'AiChat',
  data() {
    return {
      inputMessage: '',
      messages: [],
      tips: [
        {
          id: 1,
          icon: 'el-icon-chat-line-round',
          title: '智能对话',
          description: '支持自然语言交互，理解上下文'
        },
        {
          id: 2,
          icon: 'el-icon-time',
          title: '即时响应',
          description: '毫秒级响应，快速解答您的问题'
        },
        {
          id: 3,
          icon: 'el-icon-connection',
          title: '多领域知识',
          description: '覆盖多个领域的专业知识库'
        }
      ]
    }
  },
  methods: {
    sendMessage() {
      if (!this.inputMessage.trim()) return
      
      // 添加用户消息
      this.messages.push({
        type: 'user',
        content: this.inputMessage,
        timestamp: new Date()
      })

      // 模拟AI回复
      setTimeout(() => {
        this.messages.push({
          type: 'ai',
          content: '这是一个模拟的AI回复消息。',
          timestamp: new Date()
        })
        this.scrollToBottom()
      }, 1000)

      this.inputMessage = ''
    },
    clearChat() {
      this.messages = []
    },
    formatMessage(text) {
      // 可以在这里添加消息格式化逻辑
      return text
    },
    formatTime(timestamp) {
      return new Date(timestamp).toLocaleTimeString()
    },
    scrollToBottom() {
      this.$nextTick(() => {
        const container = this.$refs.messageContainer
        container.scrollTop = container.scrollHeight
      })
    }
  }
}
</script>

<style scoped>
.chat-page {
  min-height: calc(100vh - var(--header-height));
  padding: 40px 20px;
  background: linear-gradient(135deg, #f8f9fa 0%, #e9ecef 100%);
}

.chat-container {
  max-width: 1000px;
  margin: 0 auto;
  height: 70vh;
  display: flex;
  flex-direction: column;
  border-radius: var(--radius-md);
  overflow: hidden;
}

.chat-header {
  padding: 20px;
  display: flex;
  justify-content: space-between;
  align-items: center;
  border-bottom: 1px solid rgba(99, 102, 241, 0.1);
}

.ai-status {
  display: flex;
  align-items: center;
  gap: 8px;
  color: var(--text-primary);
}

.status-dot {
  width: 8px;
  height: 8px;
  background: #10B981;
  border-radius: 50%;
  display: inline-block;
  animation: pulse 2s infinite;
}

.chat-messages {
  flex: 1;
  overflow-y: auto;
  padding: 20px;
}

.welcome-message {
  text-align: center;
  padding: 40px;
  color: var(--text-secondary);
}

.ai-avatar {
  width: 60px;
  height: 60px;
  margin: 0 auto 20px;
  background: var(--primary-gradient);
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
}

.ai-avatar i {
  font-size: 30px;
  color: white;
}

.message {
  display: flex;
  gap: 12px;
  margin-bottom: 20px;
}

.message.user {
  flex-direction: row-reverse;
}

.message-avatar {
  width: 40px;
  height: 40px;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  background: var(--primary-gradient);
  flex-shrink: 0;
}

.message-avatar i {
  color: white;
  font-size: 20px;
}

.message-content {
  max-width: 70%;
}

.message.user .message-content {
  align-items: flex-end;
}

.message-text {
  padding: 12px 16px;
  border-radius: 12px;
  background: white;
  box-shadow: var(--shadow-sm);
  line-height: 1.5;
}

.message.user .message-text {
  background: var(--primary-gradient);
  color: white;
}

.message-time {
  font-size: 12px;
  color: var(--text-secondary);
  margin-top: 4px;
}

.chat-input {
  padding: 20px;
  border-top: 1px solid rgba(99, 102, 241, 0.1);
}

.input-actions {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-top: 12px;
}

.action-btn {
  color: var(--text-secondary);
}

.send-btn {
  padding: 8px 24px;
}

.features-tips {
  max-width: 1000px;
  margin: 40px auto 0;
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(250px, 1fr));
  gap: 20px;
}

.tip-card {
  padding: 20px;
  text-align: center;
  border-radius: var(--radius-md);
  transition: all 0.3s ease;
}

.tip-card:hover {
  transform: translateY(-5px);
  box-shadow: var(--shadow-lg);
}

.tip-card i {
  font-size: 32px;
  margin-bottom: 12px;
  background: var(--primary-gradient);
  -webkit-background-clip: text;
  -webkit-text-fill-color: transparent;
}

.tip-card h3 {
  margin-bottom: 8px;
  color: var(--text-primary);
}

.tip-card p {
  color: var(--text-secondary);
  font-size: 14px;
}

.glass-effect {
  background: rgba(255, 255, 255, 0.7);
  backdrop-filter: blur(10px);
  border: 1px solid rgba(99, 102, 241, 0.1);
}

@keyframes pulse {
  0% { opacity: 1; }
  50% { opacity: 0.5; }
  100% { opacity: 1; }
}

/* 响应式调整 */
@media (max-width: 768px) {
  .chat-page {
    padding: 20px 10px;
  }
  
  .message-content {
    max-width: 85%;
  }
}
</style> 